﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using RearLivestock.Common.Domains.zjy;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.ErrorCodeEnum;

namespace RearLivestock.Beginning.Api.Application.Command.PrenatalCheckup
{
    /// <summary>
    /// 获取妊检方式
    /// </summary>
    public class SearchPregnancyTypeHandler : IRequestHandler<SearchPregnancyTypeCommand, Result>
    {
        /// <summary>
        /// 妊检方式仓储
        /// </summary>
        private readonly IBaseRepository<PregnancyTestType> _pregnancyTestTypeRepository;

        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<SearchPregnancyTypeHandler> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="pregnancyTestTypeRepository"></param>
        /// <param name="logger"></param>
        public SearchPregnancyTypeHandler(IBaseRepository<PregnancyTestType> pregnancyTestTypeRepository,
                                          ILogger<SearchPregnancyTypeHandler> logger)
        {
            _pregnancyTestTypeRepository = pregnancyTestTypeRepository;
            _logger = logger;
        }

        /// <summary>
        /// 获取妊检方式
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(SearchPregnancyTypeCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                var query = await (await _pregnancyTestTypeRepository.GetAll()).ToListAsync();
                if (query.Count > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = query;
                    result.Msg = "获取妊检方式异常";
                }
            }
            catch (Exception ex)
            {
                result.Msg = "获取妊检方式异常" + ex.Message;
                _logger.LogError("获取妊检方式异常" + ex.Message);
            }
            return result;
        }
    }
}
